package com.alvin.demo.config;

import com.alvin.demo.realm.ShiroRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

    //将自己的验证方式加入容器
    @Bean
    public ShiroRealm myShiroRealm() {
        return new ShiroRealm();
    }

    // 防止地址栏出现jsessionid
    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 设置session过期时间3小时
        sessionManager.setGlobalSessionTimeout(3*60*60*1000L);
//        sessionManager.setGlobalSessionTimeout(5*60*1000L);
        // 为了解决输入网址地址栏出现 jsessionid 的问题
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    //权限管理，配置主要是Realm的管理认证
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        // 将 sessionManager 注入到 SecurityManager 中，否则不会生效
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> chainDefinitionMap = new LinkedHashMap<>();
//        chainDefinitionMap.put("/unauthorized", "anon"); // 配置式 没有权限时返回
        chainDefinitionMap.put("/login", "anon");
        chainDefinitionMap.put("/", "anon");
        chainDefinitionMap.put("/static/**", "anon");
        chainDefinitionMap.put("/favicon.ico", "anon");
        //对所有用户认证
        chainDefinitionMap.put("/**", "authc"); // 设置其他接口需要认证才能访问
//        chainDefinitionMap.put("/add", "perms[\"add\"]"); // 配置权限控制
//        chainDefinitionMap.put("/admin", "perms[\"add\",\"query\"]"); // 配置权限控制

        // 配置拦截器
        Map<String, Filter> filterMap = new LinkedHashMap<>();
//        filterMap.put("perms", new MyFilter()); // 过滤器只能 处理配置方式的权限， 不能出来注解的
        shiroFilterFactoryBean.setFilters(filterMap);
        //登录
        shiroFilterFactoryBean.setLoginUrl("/");
        shiroFilterFactoryBean.setSuccessUrl("/");
        //错误页面，认证不通过跳转
        // 配合配置式 设置访问权限时使用
//        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(chainDefinitionMap);
        return shiroFilterFactoryBean;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
            SecurityManager securityManager
    ) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}